﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Interfaces
Imports Lourdes.BackEnd.View
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid
Public Class frmDocumentosManager
  Dim editoresValueChange As Boolean = False
  Dim isFromConfigurarControles As Boolean = True

  Private Sub ConfigurarControles()
    editoresValueChange = False 'inicializo, valuechanged se ejecuta solo al principio.
    isFromConfigurarControles = True
    calFechaFacturaDesde.Value = Now.Subtract(New System.TimeSpan(1, 0, 0, 0))
    calFechaFacturaHasta.Value = Now
    isFromConfigurarControles = False
    'CargarFiltroEstados()
    CargarGrid()
  End Sub

  Private Sub CargarGrid()
    editoresValueChange = False
    With grdFacturas
      ' .DataSource = New List(Of Factura) 'Para limpiar la grilla entre filtrados, sino genera problemas con datos anteriores.
      '.DataBind()
      .DataSource = getData()
      .DataBind()
    End With
  End Sub

  Private Function getData() As Object
    ' Dim lstEstados As List(Of Integer) = EstadosSeleccionados()
    Return (New BC_Factura).getByFilters(IIf(txtFacturaIdDesde.Value Is Nothing, "", txtFacturaIdDesde.Value), IIf(txtFacturaIdHasta.Value Is Nothing, "", txtFacturaIdHasta.Value), _
                                         txtNombre.Text, txtApellido.Text, calFechaFacturaDesde.Value, calFechaFacturaHasta.Value)
    '(lstEstados, Utils.getIntFromEditor(txtEntregaId), Utils.getIntFromEditor(txtFacturaId))
  End Function

  Private Sub frmDocumentosManager_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ConfigurarControles()
  End Sub

  Private Sub txtApellido_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtApellido.ValueChanged
    editoresValueChange = True
  End Sub

  Private Sub txtNombre_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtNombre.ValueChanged
    editoresValueChange = True
  End Sub

  Private Sub txtApellido_AfterExitEditMode(sender As System.Object, e As System.EventArgs) Handles txtApellido.AfterExitEditMode
    If editoresValueChange Then CargarGrid()
  End Sub

  Private Sub txtNombre_AfterExitEditMode(sender As System.Object, e As System.EventArgs) Handles txtNombre.AfterExitEditMode
    If editoresValueChange Then CargarGrid()
  End Sub

  Private Sub txtFacturaIdDesde_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtFacturaIdDesde.ValueChanged
    editoresValueChange = True
  End Sub

  Private Sub txtFacturaIdDesde_AfterExitEditMode(sender As System.Object, e As System.EventArgs) Handles txtFacturaIdDesde.AfterExitEditMode
    If editoresValueChange Then CargarGrid()
  End Sub

  Private Sub txtFacturaIdHasta_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtFacturaIdHasta.ValueChanged
    editoresValueChange = True
  End Sub

  Private Sub txtFacturaIdHasta_AfterExitEditMode(sender As System.Object, e As System.EventArgs) Handles txtFacturaIdHasta.AfterExitEditMode
    If editoresValueChange Then CargarGrid()
  End Sub

  Private Sub calFechaFacturaDesde_ValueChanged(sender As System.Object, e As System.EventArgs) Handles calFechaFacturaDesde.ValueChanged
    If Not isFromConfigurarControles And editoresValueChange Then CargarGrid()
  End Sub

  Private Sub calFechaFacturaHasta_ValueChanged(sender As System.Object, e As System.EventArgs) Handles calFechaFacturaHasta.ValueChanged
    If Not isFromConfigurarControles And editoresValueChange Then CargarGrid()
  End Sub

  Private Sub calFechaFacturaDesde_BeforeDropDown(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles calFechaFacturaDesde.BeforeDropDown
    editoresValueChange = True
  End Sub

  Private Sub calFechaFacturaHasta_BeforeDropDown(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles calFechaFacturaHasta.BeforeDropDown
    editoresValueChange = True
  End Sub

  Private Sub grdFacturas_ClickCellButton(sender As System.Object, e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles grdFacturas.ClickCellButton
    Select Case e.Cell.Column.Key
      Case "Imprimir"
        Utils.ShowReport(Enums.ReporteTipos.Factura, "FacturaId", e.Cell.Row.Cells("FacturaId").Value, Me)
      Case "Editar"
        Dim oFrmFactura As New frmFactura()
        oFrmFactura.FacturaId = e.Cell.Row.Cells("FacturaId").Value
        oFrmFactura.ShowDialog(Me)
    End Select
  
  End Sub

  Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
    'If MsgBox("¿Desea salir de la bandeja de facturas?", vbYesNo, "Salir de Facturas Manager") = vbYes Then
    Me.Dispose()
    Me.Close()
    ' End If
  End Sub

  Private Sub grdFacturas_InitializeLayout(sender As System.Object, e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdFacturas.InitializeLayout   
    e.Layout.Bands(0).Columns.Item("DocNumero").Header.Caption = "Nro. de Factura"
    e.Layout.Bands(0).Columns.Item("FechaDocumento").Header.Caption = "Fecha de Factura"
  End Sub

  Private Sub frmDocumentosManager_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If Keys.Escape = e.KeyCode And btnCancelar.Enabled = True Then
      btnCancelar_Click(Nothing, Nothing)
    End If
  End Sub

End Class